package com.farent.solutions.plugins.aribaweb.editor.awl;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.text.Document;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.text.CloneableEditor;
import org.openide.text.NbDocument;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;

/**
 *
 * @author farouka
 */
public class AWLSourceEditor  extends CloneableEditor implements MultiViewElement, Runnable {

    private JComponent toolbar;

    private MultiViewElementCallback callback;

    public AWLSourceEditor() {
    }

    public AWLSourceEditor(AWLEditorSupport ed) {
        super(ed);
    }

    @Override
    public JComponent getVisualRepresentation() {
        return this;
    }

    @Override
    public JComponent getToolbarRepresentation() {
        if (toolbar == null) {
            JEditorPane jEditorPane = this.pane;
            if (jEditorPane != null) {
                Document doc = jEditorPane.getDocument();
                if (doc instanceof NbDocument.CustomToolbar) {
                    toolbar = ((NbDocument.CustomToolbar) doc).createToolbar(jEditorPane);
                }
            }
            if (toolbar == null) {
                toolbar = new JPanel();
            }
        }
        return toolbar;
    }

    @Override
    public void setMultiViewCallback(MultiViewElementCallback callback) {
        this.callback = callback;
        updateName();
        setName(this.getName());
        setDisplayName(this.getDisplayName());
        setHtmlDisplayName(this.getHtmlDisplayName());
    }

    @Override
    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;
    }

    @Override
    public void componentDeactivated() {
        super.componentDeactivated();
    }

    @Override
    public void componentActivated() {
        super.componentActivated();
    }

    @Override
    public void componentOpened() {
        super.componentOpened();
    }

    @Override
    public void componentClosed() {
        super.componentClosed();
    }

    @Override
    public void componentShowing() {
        super.componentShowing();
    }

    @Override
    public void componentHidden() {
        super.componentHidden();
    }

    @Override
    public void run() {
        MultiViewElementCallback c = callback;
        if (c == null) {
            return;
        }
        TopComponent tc = c.getTopComponent();
        if (tc == null) {
            return;
        }
        super.updateName();
        tc.setName(this.getName());
        tc.setDisplayName(this.getDisplayName());
        tc.setHtmlDisplayName(this.getHtmlDisplayName());
    }

    @Override
    public Lookup getLookup() {
        return ((AWLDataObject) ((AWLEditorSupport) cloneableEditorSupport()).getDataObject()).getNodeDelegate().getLookup();
    }

}
